<?php
class ServicesController  extends AppController {
	var $name = 'Services';
	var $components = array('Uploader.Uploader');
	var $errors = array();
	//var $uses=array('Image');

	function beforeFilter() {
		parent::beforeFilter();
		$this->Uploader->uploadDir = 'files/uploads/services/'; //thu muc chua file upload
        $this->Uploader->enableUpload = true;
        $this->Uploader->maxFileSize = '2M'; // quy dinh dung luong duoc upload len toi da la 2 Megabytes
        $this->Uploader->maxNameLength = 25;//do dai cua ten file
        $this->Uploader->tempDir = TMP;
		$this->Auth->allow('index','chitiet','cateservice');
	}

	function index($fid=NULL) {
		$this->Service->recursive = 1;
		$conds = array('Service.status' =>2,'Service.lang'=>$this->lg);
		if(!empty($fid)){
			$conds = array('Service.status' =>2,'cateservices_id'=>$fid,'Service.lang'=>$this->lg);
		}
		$this->paginate = array(
			'conditions'=>$conds,
			'limit'=>10,
			'order'=>'Service.id DESC'
		);
		$this->set('service', $this->paginate());
		//debug($this->paginate());
	}

	function cateservice($id = null){
		$this->paginate = array('fields'=>array('id','title','shortcontent','image'),'conditions'=>array('Service.status'=>2,'cateservices_id'=>$id),'limit'=>5,'order'=>'Service.id DESC');
		$lists = $this->paginate();
		$this->Cateservice->unbindModel(array('hasMany' => array('Service')));
		$cate_name = $this->Cateservice->find('all',array('conditions'=>array('Cateservice.id'=>$id)));
		$this->set(compact('lists','id','cate_name'));

	}
	function chitiet($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Không tồn tại bài viết này.', true));
			$this->redirect(array('action' => 'index'));
		}
		$conditions = array('Service.status'=>2);
		$service = $this->Service->read(null, $id);
 		$cid = $service['Service']['cateservices_id'];
		$this->Cateservice->unbindModel(array('hasMany' => array('Service')));
		$cate_name = $this->Cateservice->find('all',array('conditions'=>array('Cateservice.id'=>$cid)));
 		$this->set('service',$service);
		$otherservice = $this->Service->find('all',array('conditions'=>array('Service.id <>'=>$id,'Service.status'=>2,'cateservices_id'=>$cid),'limit'=>10,'order'=>'Service.id DESC'));
		$this->set(compact('otherservice','cate_name','cid'));
 	}

   	function admin_index() {
		$this->layout='admin';
		$this->Service->recursive = 0;
		$conditions = '';
		$cate = $this->data['Service']['cateservices'];
		$key = $this->data['Service']['key'];
 		if($key!= null){
			$conditions = array('Service.title LIKE '=>'%'.$key.'%');
		}
		elseif($cate != null){
			$conditions = array('Service.cateservices_id' => $cate);
		}
		elseif($cate != null && $key!= null){
			$conditions = array( 'Service.title LIKE '=>'%'.$key.'%');
		}
		$this->paginate = array(
			'conditions'=>$conditions,
			'order'=> 'Service.id DESC'
		);
		$this->set('services', $this->paginate());
		//debug($this->paginate);
		$cateservices = $this->Service->Cateservice->find('list');
		$this->set(compact('cateservices','cate','key'));
	}


	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Không tồn tại sản phẩm này.', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('services', $this->Service->read(null, $id));
	}

	function admin_add() {
		//debug($this->data);
		if (!empty($this->data)) {
			$filename = $this->Uploader->upload('image');
			$this->Service->create();
			$users_id = $this->Auth->user('id');
			$this->data['Service']['image']  = $filename['name'];
			if ($this->Service->save($this->data)) {
				$this->Session->setFlash(__('Đã lưu.', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Thông tin không được lưu. Vui lòng thử lại.', true));
			}
		}
		$cateservices = $this->Service->Cateservice->find('list');
		$this->set(compact('cateservices'));
	}

	 function admin_edit($id = null) {
		//debug($this->data);
		$files = $this->Service->find('list',array(
			'conditions'=>array('Service.id'=>$id),
			'fields'=>'image')
		);
		$file ='';
		$file = "files" . DS . "uploads/service" . DS .$files[$id];
		//debug($file);
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Không tồn tại bài viết này.', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			$this->data['Service']['image'] = $files[$id];
			//debug($files[$id]);
            if ($data = $this->Uploader->upload('image')) {
				//debug($data);
				$tmpname  = $data['name'];
				if(is_file($file)){
					unlink($file);
				}
				$this->data['Service']['image'] = $tmpname;
				//debug($this->data['Service']['image'] );
            }
			if ($this->Service->save($this->data)) {
				$this->Session->setFlash(__('Đã lưu.', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Thông tin không được lưu. Vui lòng thử lại.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Service->read(null, $id);
		}
		$cateservices = $this->Service->Cateservice->find('list');
		$this->set(compact('cateservices'));
		//$this->set('model',$this->uses[0]);
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Không tồn tại bài viết này !', true));
			$this->redirect(array('action'=>'index'));
		}
		if($id){
			$files = $this->Service->find('first',array(
			'conditions'=>array('Service.id'=>$id),
			'fields'=>'image')
		);
		$file = "files/uploads/service/".$files['Service']['image'];
			if(is_file($file)){
				unlink($file);
			}
		}
		if ($this->Service->delete($id)) {
			$this->Session->setFlash(__('Đã xóa !', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Có lỗi xảy ra. Chưa xóa được !', true));
		$this->redirect(array('action' => 'index'));
	}
	function admin_mutildelete($str = null){
        if($str){
            $arrid=explode(',',$str);
        }else{
    		$this->Session->setFlash(__('Có lỗi xảy ra. Chưa xóa được !', true));
    		$this->redirect(array('action' => 'index'));
        }
         foreach($arrid as $item){
			if($item != null){
				$files = $this->Service->find('first',array(
				'conditions'=>array('Service.id'=>$item),
				'fields'=>'image')
				);
				$file = "files/uploads/service/".$files['Service']['image'];
				if(is_file($file)){
					unlink($file);
				}
			}
    		$this->Service->delete($item);
         }
		$this->Session->setFlash(__('Đã xóa !', true));
		$this->redirect(array('action' => 'index'));
    }
	function admin_delfile($id = null){
		$files = $this->Service->find('first',array(
			'conditions'=>array('Service.id'=>$id),
			'fields'=>'image')
			);
		$file = "files/uploads/service/".$files['Service']['image'];
		if(is_file($file)){
			unlink($file);
		}
		$this->Service->query("Update tblservices set image = '' where id='".$id."'");
    }

}
?>